package FlowGram.Application.Parser.Actions;

import java.awt.Color;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import FlowGram.Application.Parser.FinchParserException;
import FlowGram.Application.Parser.Controllers.FinchController;


/**
 * 
 * @author thackray
 *
 */
public class BeakColor extends FinchAction {

	
	protected Color color;
	protected int time; //milliseconds
	protected boolean usetime;
	
	public BeakColor(){
		this.color = new Color(0, 0, 0);
		this.time = (5*1000); //5 seconds
	}
	
	public void setTime(int time){
		this.time = time;
	}
	public int getTime(){
		return this.time;
	}
	public void setColor(Color c){
		this.color = c;
	}
	public Color getColor(){
		return this.color;
	}
	public boolean getUseTime(){
		return this.usetime;
	}
	public void setUseTime(boolean use){
		this.usetime = true;
	}
	
	
	
	public static boolean matches(String s){
		boolean result = s.matches("^beakcolor\\s+.*");
		return result;
	}
	public static String syntax(){
		return "beakcolor <color> [<time(s)=5.0>]";
	}
	@Override
	public String toCode(int indent) {
		String code = "beakcolor "+this.colorToName(this.color);
		if(this.usetime){
			code += " "+(((double)this.time)/1000);
		}
		return code;
	}
	@Override
	public String toHTML(String idPrefix){
		return this.toCode();
	}
	@Override
	public void execute(FinchController controller) {
		if(this.usetime){
			controller.setColor(this.color, this.time);
		}else{
			controller.setColor(this.color);
		}
	}

	@Override
	public String logMessage() {
		return "Setting beak color to "+this.colorToName(this.color);
	}

	@Override
	public void parse(String statement) throws FinchParserException {
		Pattern p = Pattern.compile("^beakcolor\\s+([a-z]+|(\\d{1,3}),(\\d{1,3}),(\\d{1,3}))((?:\\s+)[\\d\\.]+)?");
		Matcher matcher = p.matcher(statement);
		while(matcher.find()){
			if(matcher.group(1) != null){
				if(matcher.group(2) != null && matcher.group(3) != null && matcher.group(4) != null){
					this.color = new Color(Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)));
				}else{
					try {
					    Field field = Class.forName("java.awt.Color").getField(matcher.group(1));
					    this.color = (Color)field.get(null);
					} catch (Exception e) {
					    this.color = null; // Not defined
					}

					//this.color = Color.getColor(matcher.group(1));
				}
				if(matcher.group(5) != null){
					this.time = (int)(Double.parseDouble(matcher.group(5))*1000);
					this.usetime = true;
				}
			}else{
				
			}
		}
		if(this.color == null){
			throw new FinchParserException("Could not understand the color!");
		}

	}

	
	protected String colorToName(Color c){
		if(c.equals(Color.BLACK) || c.equals(Color.black)){
			return "black";
		}else if(c.equals(Color.BLUE) || c.equals(Color.blue)){
			return "blue";
		}else if(c.equals(Color.CYAN) || c.equals(Color.cyan)){
			return "cyan";
		}else if(c.equals(Color.DARK_GRAY) || c.equals(Color.darkGray)){
			return "darkgray";
		}else if(c.equals(Color.GRAY) || c.equals(Color.gray)){
			return "gray";
		}else if(c.equals(Color.GREEN) || c.equals(Color.green)){
			return "green";
		}else if(c.equals(Color.LIGHT_GRAY) || c.equals(Color.lightGray)){
			return "lightgray";
		}else if(c.equals(Color.MAGENTA) || c.equals(Color.magenta)){
			return "magenta";
		}else if(c.equals(Color.ORANGE) || c.equals(Color.orange)){
			return "orange";
		}else if(c.equals(Color.PINK) || c.equals(Color.pink)){
			return "pink";
		}else if(c.equals(Color.RED) || c.equals(Color.red)){
			return "red";
		}else if(c.equals(Color.WHITE) || c.equals(Color.white)){
			return "white";
		}else if(c.equals(Color.YELLOW) || c.equals(Color.yellow)){
			return "yellow";
		}else{
			return c.getRed()+","+c.getGreen()+","+c.getBlue();
		}
	}
	
}
